package javaTest04.util;

import javaTest04.exercise02.annotation.Column;
import javaTest04.exercise02.annotation.Entity;

import java.lang.reflect.Field;

public class SqlUtils {

    public static final Class<Entity> ENTITY_CLASS = Entity.class;

    public static final Class<Column> COLUMN_CLASS = Column.class;

    public static final String BLANK = " ";

    public SqlUtils() {
    }

    public static String getFiledType(Field field, int maxlength) {
        String res = "";
        switch (field.getType().getSimpleName()) {
            case "String":
                res = "VARCHAR(" + maxlength + ")";
                break;
            case "Integer":
            case "Boolean":
                res = "INTEGER(" + maxlength + ")";
                break;
            case "Double":
            case "Float":
                res = "DECIMAL(" + maxlength + ",3)";
                break;
            case "Character":
                res = "CHAR";
                break;
            case "Long":
                res = "BIGINT(" + maxlength + ")";
                break;
        }
        return res;
    }

    public static String generateCreateTableSql(Class<?> clazz) {//生成建表SQL语句

        Entity entity = clazz.getAnnotation(ENTITY_CLASS);

        String tableName = entity.value();//表名

        Field[] fields = clazz.getDeclaredFields();

        StringBuilder tableContent = new StringBuilder();

        for (int i = 0; i < fields.length; i++) {//拼接建表属性sql
            Field field = fields[i];

            if (!isColumn(field))
                continue;
            StringBuilder line = new StringBuilder();

            Column column = field.getAnnotation(COLUMN_CLASS);
            String fieldName = column.value();
            String fieldType = getFiledType(field, column.maxLength());

            line.append(fieldName).append(BLANK).append(fieldType);

            if (!column.nullable())
                line.append(BLANK).append("NOT NULL");

            if (i < fields.length - 1)
                line.append(",");

            tableContent.append(line);
        }

        return String.format("CREATE TABLE %s (%s);", tableName, tableContent);
    }


    public static String generateDeleteTableSql(Class<?> clazz){
        return "DROP TABLE IF EXISTS "+clazz.getAnnotation(ENTITY_CLASS).value()+";";
    }

    public static String generateAlterTableSql(Class<?> clazz){
        return String.format("ALTER TABLE %s CHANGE name myname VARCHAR(10)",clazz.getAnnotation(ENTITY_CLASS).value());
    }

    public static String generateDescTableSql(Class<?> clazz){
        return "DESC "+clazz.getAnnotation(ENTITY_CLASS).value()+";";
    }

    public static boolean isEntity(Class<?> clazz) {
        return clazz.getAnnotation(ENTITY_CLASS) != null && !clazz.getAnnotation(ENTITY_CLASS).value().equals("");
    }

    public static boolean isColumn(Field field) {
        return field.getAnnotation(COLUMN_CLASS) != null && !field.getAnnotation(COLUMN_CLASS).value().equals("");
    }


}
